package com.webproject.action.sysManager;

import com.webproject.core.BaseAction;
import com.webproject.core.Page;
import com.webproject.entity.sysManager.User;
import com.webproject.entity.sysManager.UserRoleRelation;
import com.webproject.service.sysManager.UserManagerService;

/**
 * user add,update,del,query 
 * @author lijiang
 *
 */
public class UserManagerAction extends BaseAction {

	private Page mpage;
	public Page getMpage() {
		return mpage;
	}
	public void setMpage(Page mpage) {
		this.mpage = mpage;
	}
	private UserManagerService userManagerService;
	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}
	public String queryUser(){
		mpage=getPage();
		userManagerService.queryUser(mpage);
		setTotal(mpage);
		return SUCCESS;
	}
	public String editUser(){
		User user=requestUser();
		if(oper.equals("add")){
			userManagerService.saveUser(user);
		}else if(oper.equals("edit")){
			userManagerService.updateUser(user);
		}else if(oper.equals("del")){
			userManagerService.delUser(user);
		}
		return SUCCESS;
	}
	public User requestUser(){
		User user=new User();
		String id=getRequest().getParameter("id");
		if(isNotEmpty(id)){
			user.setId(Integer.valueOf(id));
		}
		String name=getRequest().getParameter("name");
		user.setName(name);
		String phone=getRequest().getParameter("phone");
		user.setPhone(phone);
		String mobile=getRequest().getParameter("mobile");
		user.setMobile(mobile);
		String zip=getRequest().getParameter("zip");
		user.setZip(zip);
		String address=getRequest().getParameter("address");
		String pwd=getRequest().getParameter("pwd");
		String loginName=getRequest().getParameter("loginName");
		String isuse=getRequest().getParameter("isuse");
		user.setIsuse(Boolean.valueOf(isuse));
		user.setLoginName(loginName);
		user.setPwd(pwd);
		user.setAddress(address);
		return user;
	}
	public String addUserRoleRelation(){
		UserRoleRelation relation=getUserRole();
		userManagerService.saveUserRoleRelation(relation);
		return SUCCESS;
	}
	public String delUserRoleRelation(){
		UserRoleRelation relation=getUserRole();
		userManagerService.delUserRoleRelation(relation);
		return SUCCESS;
	}
	
	private UserRoleRelation getUserRole(){
		UserRoleRelation relation=new UserRoleRelation();
		relation.setRoleId(Integer.valueOf(getRequest().getParameter("roleId")));
		relation.setUserId(Integer.valueOf(getRequest().getParameter("userId")));
		return relation;
	}
	
}
